//
//  ZZHttpRequest.swift
//  QMXK
//
//  Created by jenkin on 2018/12/4.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit
import Moya
import SwiftyJSON
typealias SuccessBlock = (_ data:Dictionary<String, Any>) -> Void
typealias FailureBlock = (_ data:Dictionary<String, Any>) -> Void

class ZZHttpRequest: NSObject {
    static let shareRequest:ZZHttpRequest = ZZHttpRequest.init()
    let requestProvier =  MoyaProvider<MoyaApi>()
    let gpRequestProvier =  MoyaProvider<GupiaoAPI>()
    func request(target:MoyaApi, successBlock:@escaping SuccessBlock, failureBlock:@escaping FailureBlock){
        self.requestProvier.request(target, callbackQueue: nil, progress: { (response) in
            
        }) { (result) in
            switch result{
                case let .success(result):
                    do{
                        try
                            successBlock(result.mapJSON() as! Dictionary<String, Any>)
                    }catch{
                        failureBlock(["code":"100"])
                    }
                    break
                case .failure(_):
                    failureBlock(["code":"100"])
                    break
            }
        }
    }
    
    func gpRequest(target:GupiaoAPI, successBlock:@escaping SuccessBlock, failureBlock:@escaping FailureBlock){
        self.gpRequestProvier.request(target, callbackQueue: nil, progress: { (response) in
            
        }) { (result) in
            switch result{
            case let .success(result):
                do{
                    try
                        successBlock(result.mapJSON() as! Dictionary<String, Any>)
                }catch{
                    failureBlock(["code":"100"])
                }
                break
            case .failure(_):
                failureBlock(["code":"100"])
                break
            }
        }
    }
}



